package jml.defaultimple;

import java.util.ArrayList;

import jml.config.Config;
import jml.def.Filter;
import jml.def.Label;
import jml.def.Library;
import jml.def.Plugin;
import jml.def.Title;
import jml.defaultimple.util.JMLLoader;

public class LibraryImple implements Library
{
	private String name;
	private ArrayList<Filter> filters;
	private ArrayList<Plugin> plugins;
	
	public void addPlugin(Plugin p)
	{
		plugins.add(p);
	}
	
	public LibraryImple()
	{
		filters = new ArrayList<Filter>();
	}
	
	public void addFilter(Filter f)
	{
		filters.add(f);
	}
	
	@Override
	public Filter[] getFilters()
	{
		Filter[] sfilters = new Filter[filters.size()];
		return filters.toArray(sfilters);
	}
	
	public ArrayList<Filter> getFiltersList()
	{
		return filters;
	}

	@Override
	public Filter getFilter(String filtername)
	{
		for(Filter f:filters)
		{
			if(f.getName().equals(filtername))
			{
				return f;
			}
		}
		
		return null;
	}

	@Override
	public Title[] getAllTitles()
	{
		ArrayList<Title> allTittles = new ArrayList<Title>();
		for(Filter filter:filters)
		{
			for(Label label:filter.getLabels())
			{
				for(Title tittle:label.getTitles())
				{
					if( !allTittles.contains(tittle) )
					{
						allTittles.add(tittle);
					}
				}
			}
		}
		
		Title all[]=new Title[allTittles.size()];
		return allTittles.toArray(all);
	}
	
	@Override
	public Title getTitleByPath(String path)
	{
		Title[] titles = getAllTitles();
		for(int i=0; i<titles.length; i++)
		{
			if( titles[i].getPath().equals(path) )
			{
				return titles[i];
			}
		}
		return null;
	}	

	
	public void setName(String name)
	{
		this.name=name;
	}

	@Override
	public String getName()
	{
		return name;
	}

	@Override
	public String[] getMediaFolders()
	{
		return Config.getConfig(false).getLibrary().getFolders();
	}
	

	public static void main(String[] args)
	{
		
		Library lib = JMLLoader.load("WebContent/jmusiclibrary.cfg.xml");
		Title t = lib.getTitleByPath("G:\\AUDIO2\\ALBUMS\\George Harrison\\Allthing Must Pass");
	}
}
